package com.example.beryl.http;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.kingdee.bos.webapi.sdk.AppCfg;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;

public class Net_LogMethod{

    JSONArray jsonArray = null;
    private static String IP = "192.168.1.161";
    private static String DBName = "BERYL_AMS20200702";
    private static String USER = "sa";
    private static String PWD = "admin@123";
    public Net_LogMethod() {
    }
    private static Connection getSQLConnection() {
        Connection con = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            //加上 useunicode=true;characterEncoding=UTF-8 防止中文乱码
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + IP + ":1433/" + DBName + ";useunicode=true;characterEncoding=UTF-8", USER, PWD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /** 查询数据库 */
    public static String Query() {
        String result = "";
        try {
            Connection conn = getSQLConnection();
            String sql = "select * from Sys_PsnNum where cPsn_Num = '0005'";
            Statement stmt = conn.createStatement();//
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String s1 = rs.getString("cPsn_Num");
                String s2 = rs.getString("cPsn_Name");
                result += s1 + "  -  " + s2 + "\n";
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            result += "查询数据异常!" + e.getMessage();
        }
        return result;
    }

    public  void Insert(String json)
    {
        try {
            try {
                jsonArray = new JSONArray(json);
            }catch (JSONException e)
            {
                e.printStackTrace();
            }
            Connection conn = getSQLConnection();
            String sql = String.format("insert Into Android_Log (accout,barcode,LoginUser,Type,MAC,action,Message,Time) Values('%s','%s','%s','%s','%s','%s','%s','%s')",
                    jsonArray.getJSONObject(0).get("accout").toString(),
                    jsonArray.getJSONObject(0).get("barcode").toString(),
                    jsonArray.getJSONObject(0).get("LoginUser").toString(),
                    jsonArray.getJSONObject(0).get("Type").toString(),
                    jsonArray.getJSONObject(0).get("MAC").toString(),
                    jsonArray.getJSONObject(0).get("action").toString(),
                    jsonArray.getJSONObject(0).get("Message").toString(),
                    jsonArray.getJSONObject(0).get("Time").toString());
            Statement stmt = conn.createStatement();//
            stmt.executeUpdate(sql);
        }catch (SQLException e)
        {
            e.printStackTrace();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

}
